package o;

import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.impl.DownloadTask;
import com.huawei.fastengine.fastview.download.utils.PackageUtils;
import com.huawei.hms.framework.network.grs.GrsApi;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Map.Entry;

public class cmw extends cmu {
    private String ˊ;
    private long ˎ;
    private long ˏ;
    private int ॱ;
    private int ᐝ;

    class e extends Thread {
        final /* synthetic */ cmw ˊ;
        private String ˏ;

        e(cmw o_cmw, String str) {
            this.ˊ = o_cmw;
            this.ˏ = str;
        }

        public void run() {
            InputStream inputStream = null;
            try {
                this.ˊ.ˏ = System.currentTimeMillis();
                URLConnection openConnection = new URL(this.ˏ).openConnection();
                openConnection.setUseCaches(false);
                openConnection.setConnectTimeout(10000);
                openConnection.connect();
                inputStream = openConnection.getInputStream();
                byte[] bArr = new byte[WXMediaMessage.DESCRIPTION_LENGTH_LIMIT];
                while (inputStream.read(bArr) != -1 && !Thread.currentThread().isInterrupted()) {
                    this.ˊ.ॱ = this.ˊ.ॱ + 1;
                }
                inputStream.close();
                this.ˊ.ˎ = System.currentTimeMillis();
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        cmp.ˊ.ˊ(DownloadTask.TABLE_NAME, "close input stream failed: " + e.toString());
                    }
                }
            } catch (SocketTimeoutException e2) {
                this.ˊ.ˎ = 0;
                cmp.ˊ.ˊ(DownloadTask.TABLE_NAME, "timeout error: " + e2.toString());
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e3) {
                        cmp.ˊ.ˊ(DownloadTask.TABLE_NAME, "close input stream failed: " + e3.toString());
                    }
                }
            } catch (IOException e32) {
                this.ˊ.ˎ = 0;
                cmp.ˊ.ˊ(DownloadTask.TABLE_NAME, "connect error: " + e32.toString());
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e322) {
                        cmp.ˊ.ˊ(DownloadTask.TABLE_NAME, "close input stream failed: " + e322.toString());
                    }
                }
            } catch (Throwable th) {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e4) {
                        cmp.ˊ.ˊ(DownloadTask.TABLE_NAME, "close input stream failed: " + e4.toString());
                    }
                }
            }
        }
    }

    public cmw(cmq o_cmq) {
        super(o_cmq, DownloadTask.TABLE_NAME);
    }

    public void run() {
        String str;
        if (this.ˋ.ˎ().getPackageName().equals("com.huawei.appmarket")) {
            str = "com.huawei.appmarketV05";
        } else if (this.ˋ.ˎ().getPackageName().equals(PackageUtils.HWGAMEBOX_PACKAGE_NAME)) {
            str = "com.huawei.gameboxV01";
        } else {
            str = "com.huawei.appmarketV05";
        }
        Map synGetGrsUrls = GrsApi.synGetGrsUrls(str);
        if (synGetGrsUrls != null) {
            for (Entry entry : synGetGrsUrls.entrySet()) {
                if (((String) entry.getKey()).equals("AHEADCONN")) {
                    this.ˊ = (String) entry.getValue();
                    break;
                }
            }
        }
        if (TextUtils.isEmpty(this.ˊ)) {
            this.ˊ = HwAccountConstants.NULL;
            cmp.ˊ.ˋ(DownloadTask.TABLE_NAME, "query url from grs failed");
        } else {
            this.ˊ = ˏ(this.ˊ);
            e eVar = new e(this, this.ˊ);
            eVar.start();
            try {
                eVar.join(20000);
            } catch (InterruptedException e) {
                cmp.ˊ.ˋ(DownloadTask.TABLE_NAME, e.toString());
            }
            this.ᐝ = (this.ॱ * 1000) / ((int) (this.ˎ - this.ˏ));
        }
        this.ˋ.ˊ(-107, this.ᐝ > 30 ? 2 : 1, ˊ());
    }

    private String ˏ(String str) {
        if (str != null) {
            return str.substring(0, str.lastIndexOf("/") + 1) + "diagnose.apk";
        }
        return null;
    }

    private String ˊ() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Download Test\n");
        stringBuilder.append("[url]:[").append(this.ˊ).append("]\n");
        stringBuilder.append("[speed]:[").append(this.ᐝ).append("kB/s]\n");
        return stringBuilder.toString();
    }
}
